package com.erp.servlet;

import com.erp.bean.Emp;
import com.erp.service.EmpService;
import com.erp.service.impl.EmpServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zch
 * 修改密码控制器
 */
@WebServlet("/empupdate.action")
public class EmpUpdatePassServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建用户业务逻辑对象
        EmpService empService = new EmpServiceImpl();
        //接收信息
        String oldpass = req.getParameter("oldpass");
        String newpass = req.getParameter("newpass");
        //获得会话
        HttpSession session = req.getSession();
        //从会话中取得user对象
        Emp emp = (Emp) session.getAttribute("currentUser");
        System.out.println(emp);
        if(oldpass.equals(emp.getPass())){
            //调用user的set方法将新密码存入user对象中
            emp.setPass(newpass);
            //使用修改方法
            empService.update(emp);
            //重定向到登录页面
            resp.sendRedirect("login.action");
        }else {
            //登录失败
            String mess = "用户名或密码错误！";
            req.setAttribute("mess",mess);
            //返回登录界面，转发（将信息传递到页面中）
            req.getRequestDispatcher("empupdatepass.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
